﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string uid = txtUsername.Text;
            string pwd = txtPassword.Text;
            string repwd = txtRepassword.Text;

            string cmdString = "select * from Users";

            var dataSet = DbHelper.GetDataSet(cmdString);

            if (uid == dataSet.Tables[0].Rows[0]["Username"].ToString())
            {
                MessageBox.Show("用户已存在 请重新输入", "提示");
            }
            else if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(pwd) && pwd == repwd)
            {
                MessageBox.Show("注册成功","提示");
                this.Close();
                string insert = string.Format("insert into Users (Username, Password) values ('{0}','{1}')", uid, pwd);
                DbHelper.AddOrEditOrDeleSave(insert);
            }
            else if (txtUsername.Text.ToString().Trim()=="")
            {
                MessageBox.Show("用户名为空 请重新填写", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUsername.Focus();
            }
            else if (pwd != repwd)
            {
                MessageBox.Show("密码不一致 请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Focus();
            }

        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
